package com.slz3.business.module.goodsManagement.gooAttribute.service;

import com.slz3.business.frame.enums.ValidFlag;
import com.slz3.business.frame.module.DomainEntity;
import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.module.Services;
import com.slz3.business.frame.steward.PageableSteward;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.goodsManagement.gooAttribute.domain.GooAttribute;
import com.slz3.business.module.goodsManagement.gooAttribute.domain.GooAttributeFactory;
import com.slz3.business.module.goodsManagement.gooAttribute.steward.GooAttributeSpecification;
import com.slz3.business.module.goodsManagement.gooTypeLevel1.domain.GooTypeLevel1;
import com.slz3.business.module.goodsManagement.gooTypeLevel1.steward.GooTypeLevel1Specification;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.domain.GooTypeLevel2;
import com.slz3.business.module.goodsManagement.gooTypeLevel2.steward.GooTypeLevel2Specification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.repository.support.PageableExecutionUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GooAttributeServiceImpl implements GooAttributeService {
    @Override
    public GooAttribute save(GooAttribute gooAttribute) {
        return Repositorys.gooAttribute.save(GooAttributeFactory.initBaseFields(gooAttribute));
    }
    @Override
    public Page<GooAttribute> page(MultiValueMap<String, String> params) {
        //根据类别模糊查询, 一级分类, 二级分类
        Map<String, List<String>> map = new HashMap<>();
        MultiValueMap<String, String> multiParams = CollectionUtils.toMultiValueMap(map);
        params.forEach(multiParams::put);

        if (VE.isNotEmpty(params.getFirst("gooTypeName"))) {
            List<GooTypeLevel1> typeLevel1s = Repositorys.gooTypeLevel1.findAll(GooTypeLevel1Specification.findJoin(params));
            List<GooTypeLevel2> typeLevel2s = Repositorys.gooTypeLevel2.findAll(GooTypeLevel2Specification.findJoin(params));
            //如果没有根据名称查询到类别就返回空Page
            if (typeLevel1s.isEmpty() && typeLevel2s.isEmpty()) {
                return PageableExecutionUtils.getPage(new ArrayList<GooAttribute>(), PageableSteward.extract(multiParams),
                        () -> 0);
            }
            List<String> typeLevel1UUIDs = typeLevel1s.stream().map(DomainEntity::getUuid).map(UUID::toString).collect(Collectors.toList());
            List<String> typeLevel2UUIDs = typeLevel2s.stream().map(DomainEntity::getUuid).map(UUID::toString).collect(Collectors.toList());
            multiParams.put("type1UUIDs", typeLevel1UUIDs);
            multiParams.put("type2UUIDs", typeLevel2UUIDs);
        }


        Page<GooAttribute> gooAttributePage = Repositorys.gooAttribute.findAll(
                GooAttributeSpecification.findJoin(multiParams), PageableSteward.extract(multiParams)
        );
        Map<UUID, GooTypeLevel1> level1Map = Repositorys.gooTypeLevel1.findAllMapByUuid();
        Map<UUID, GooTypeLevel2> level2Map = Repositorys.gooTypeLevel2.findAllMapByUuid();

        gooAttributePage.getContent().forEach(item -> item.setLevel1(level1Map.get(item.getFkTypeLevel1())));
        gooAttributePage.getContent().forEach(item -> item.setLevel2(level2Map.get(item.getFkTypeLevel2())));

        return gooAttributePage;
    }


    @Override
    public GooAttribute modify(GooAttribute gooAttributeNew) {
        return Repositorys.gooAttribute.saveAndFlush(GooAttributeFactory.initBaseFields(gooAttributeNew));
    }

    @Override
    public Optional<GooAttribute> single(UUID uuid) {
        return Repositorys.gooAttribute.findById(uuid);
    }

    @Override
    public List<GooAttribute> findAllByUuids(Iterable<UUID> uuids) {
        return Repositorys.gooAttribute.findAllById(uuids);
    }

    @Override
    public List<GooAttribute> delete(List<GooAttribute> gooAttributeList) {
        gooAttributeList.stream().forEach(item -> {
            item.setValidFlag(ValidFlag.DEL);
            GooAttributeFactory.initBaseFields(item);
        });
        return Services.gooAttributeTransactional.batchModify(gooAttributeList);
    }

}
